/***********************************************
 @Name :        qian
 @Time :        2025-06-22
 @Email :       13347217372@163.com
 @Comment :     功能窗口 天气预报
 @FileName :    widget_funcd_weather.cpp
 @Version :     1.0
************************************************/


#include "widget_funcd_weather.h"
#include "ui_widget_funcd_weather.h"
#include "search_city.h"


Widget_funcD_Weather::Widget_funcD_Weather(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcD_Weather),
    m_netManager(new QNetworkAccessManager(this))  // 初始化指针！ 初始化网络管理器
  
{   
    ui->setupUi(this);

    //搜索城市
    if(!ui->widget->layout()){
        ui->widget->setLayout(new QVBoxLayout());
        ui->widget->layout()->setContentsMargins(0,0,0,0);
    }
    Search_city * searchCity=new Search_city(ui->widget);
    
    ui->widget->layout()->addWidget(searchCity);
    
    //高温 低温折线图
    if(!ui->widget_3->layout()){
        ui->widget_3->setLayout(new QVBoxLayout());
        ui->widget_3->layout()->setContentsMargins(0,0,0,0);
    }
    weather_lable * weatherlable=new weather_lable(ui->widget_3);
    
    ui->widget_3->layout()->addWidget(weatherlable);
    
    
    // 连接信号
    connect(searchCity, &Search_city::cityIdChanged, this, &Widget_funcD_Weather::onCityIdReceived);
    connect(m_netManager, &QNetworkAccessManager::finished,this, &Widget_funcD_Weather::onNetworkReply);
    
    // 默认加载长沙天气
    requestWeather("101250101");
    
    ui->label->setStyleSheet("background-color: #F6FAFE;""border-radius: 10px;  ");
    ui->label_2->setStyleSheet("background-color: #F6FAFE;""border-radius: 10px;  ");
    ui->label_3->setStyleSheet("background-color: #F6FAFE;""border-radius: 10px;  ");
    ui->label_4->setStyleSheet("background-color: #F6FAFE;""border-radius: 10px;  ");
    ui->label_5->setStyleSheet("background-color: #F6FAFE;""border-radius: 10px;  ");
    
    //===============================图标初始化===============================
    //湿度图标
    ui->label_humidity_icon->setPixmap(QPixmap(":/img/weather/humidity.png"));
    ui->label_humidity_icon->setFixedSize(30, 30);
    ui->label_humidity_icon->setScaledContents(true);
    
    ui->label_wind_icon->setPixmap(QPixmap(":/img/weather/wind.png"));
    ui->label_wind_icon->setFixedSize(30, 30);
    ui->label_wind_icon->setScaledContents(true);
    
    ui->label_sunrise_icon->setPixmap(QPixmap(":/img/weather/sunrise.png"));
    ui->label_sunrise_icon->setFixedSize(30, 30);
    ui->label_sunrise_icon->setScaledContents(true);
    
    ui->label_sunset_icon->setPixmap(QPixmap(":/img/weather/sunset.png"));
    ui->label_sunset_icon->setFixedSize(30, 30);
    ui->label_sunset_icon->setScaledContents(true);
    
    ui->label_air_quality_icon->setPixmap(QPixmap(":/img/weather/air_quality.png"));
    ui->label_air_quality_icon->setFixedSize(25, 25);
    ui->label_air_quality_icon->setScaledContents(true);
    
    ui->label_air_quality25_icon->setPixmap(QPixmap(":/img/weather/PM2.5.png"));
    ui->label_air_quality25_icon->setFixedSize(25, 25);
    ui->label_air_quality25_icon->setScaledContents(true);
    
    ui->label_air_quality10_icon->setPixmap(QPixmap(":/img/weather/PM10.png"));
    ui->label_air_quality10_icon->setFixedSize(25, 25);
    ui->label_air_quality10_icon->setScaledContents(true);
    
    ui->label_update_time_icon->setPixmap(QPixmap(":/img/weather/updatetime.png"));
    ui->label_update_time_icon->setFixedSize(25, 25);
    ui->label_update_time_icon->setScaledContents(true);
}

Widget_funcD_Weather::~Widget_funcD_Weather()
{
    delete ui;
}

// 接收搜索到的城市ID，发起请求
void Widget_funcD_Weather::onCityIdReceived(const QString &cityId) {
    if (!cityId.isEmpty()) {
        requestWeather(cityId);
    }
}

// 发起网络请求
void Widget_funcD_Weather::requestWeather(const QString &cityId) {
    QUrl url(QString("http://t.weather.itboy.net/api/weather/city/%1").arg(cityId));
    m_netManager->get(QNetworkRequest(url));
}

// 处理网络响应
void Widget_funcD_Weather::onNetworkReply(QNetworkReply *reply) {
    if (reply->error()!= QNetworkReply::NoError) {
        QMessageBox::warning(this, "错误", "天气数据请求失败");
        reply->deleteLater();
        return;
    }

    // 保存JSON数据并显示
    m_jsonData = reply->readAll();
    reply->deleteLater();
    showWeather();
}

// 解析并显示天气信息
void Widget_funcD_Weather::showWeather() {
    if (!m_weatherInfo.loadFromJson(QString::fromUtf8(m_jsonData))) {
        QMessageBox::warning(this, "错误", "天气数据解析失败");
        return;
    }

    QList<Forecast> forecastList = m_weatherInfo.forecastList();
        if (forecastList.isEmpty()) {  // 检查列表是否为空
            QMessageBox::warning(this, "错误", "未获取到天气数据");
            return;
        }
    
    // 获取今日天气
    const Forecast today = forecastList.first();
    
    QFont font;
    font.setPointSize(12); // 设置字体大小为12号
    font.setFamily("楷体"); 
    
    ui->label_temp->setText(m_weatherInfo.wendu());
    ui->label_temp->setFont(font);
    ui->label_weather_type->setText(today.type);
    ui->label_weather_type->setFont(font);
    ui->label_humidity->setText(m_weatherInfo.shidu());
    ui->label_humidity->setFont(font);
    ui->label_fl->setText(today.fl);
    ui->label_fl->setFont(font);
    ui->label_fx->setText(today.fx);
    ui->label_fx->setFont(font);
    ui->label_sunrise_time->setText(today.sunrise);
    ui->label_sunrise_time->setFont(font);
    ui->label_sunset_time->setText(today.sunset);
    ui->label_sunset_time->setFont(font);
    ui->label_air_quality->setText(m_weatherInfo.quality());
    ui->label_air_quality->setFont(font);
    // 转换为 QString（保留 1 位小数，可根据需求调整）
    QString pm25Str = QString::number(m_weatherInfo.pm25(), 'f', 1); 
    QString pm10Str = QString::number(m_weatherInfo.pm10(), 'f', 1); 
    // 设置到 QLabel
    ui->label_air_quality25->setText(pm25Str);
    ui->label_air_quality25->setFont(font);
    ui->label_air_quality10->setText(pm10Str);
    ui->label_air_quality10->setFont(font);
    ui->label_date->setText(today.ymd);
    ui->label_date->setFont(font);
    ui->label_week->setText(today.week);
    ui->label_week->setFont(font);
    ui->label_update_time->setText(m_weatherInfo.updateTime());
    ui->label_update_time->setFont(font);
    
    QFont notice_font;
    notice_font.setPointSize(20); // 设置字体大小为16号
    notice_font.setFamily("楷体"); 
    ui->label_notice->setAlignment(Qt::AlignCenter);
    // 设置文字颜色
    QPalette palette = this->palette();
    // 设置为红色
    palette.setColor(QPalette::WindowText, QColor(255, 0, 0)); 
    ui->label_notice->setPalette(palette);
    ui->label_notice->setText(today.notice);
    ui->label_notice->setFont(notice_font);
    
    // ================= 显示当前天气图标 =================
    QPixmap currentIcon = m_weatherInfo.getCurrentWeatherIcon(); // 尺寸
    ui->label_weather_icon->setPixmap(currentIcon);
    ui->label_weather_icon->setScaledContents(true);
    ui->label_weather_icon->setFixedSize(50, 50);
    
    //未来7天天气预报
    for (int i = 0; i < forecastList.size(); i++) {
        const Forecast forecast = forecastList[i];
        if(i==0){
            ui->label_week_day1->setText(forecast.week);
            ui->label_week_day1->setFont(font);
            ui->label_weather_icon_day1->setPixmap(m_weatherInfo.getForecastWeatherIcon(i, 50));
            ui->label_weather_icon_day1->setScaledContents(true);
            ui->label_low_day1->setText(forecast.low);
            ui->label_low_day1->setFont(font);
            ui->label_high_day1->setText(forecast.high); 
            ui->label_high_day1->setFont(font);
            ui->label_low_day1->setMinimumSize(50, 50);
            ui->label_high_day1->setMinimumSize(50, 50);
        }
        if(i==1){
            ui->label_week_day2->setText(forecast.week);
            ui->label_week_day2->setFont(font);
            ui->label_weather_icon_day2->setPixmap(m_weatherInfo.getForecastWeatherIcon(i, 50));
            ui->label_weather_icon_day2->setScaledContents(true);
            ui->label_low_day2->setText(forecast.low);
            ui->label_low_day2->setFont(font);
            ui->label_low_day2->setMinimumSize(50, 50);
            ui->label_high_day2->setMinimumSize(50, 50);
            ui->label_high_day2->setText(forecast.high);
            ui->label_high_day2->setFont(font);
        }
        if(i==2){
            ui->label_week_day3->setText(forecast.week);
            ui->label_week_day3->setFont(font);
            ui->label_weather_icon_day3->setPixmap(m_weatherInfo.getForecastWeatherIcon(i, 50));
            ui->label_weather_icon_day3->setScaledContents(true);
            ui->label_low_day3->setText(forecast.low);
            ui->label_low_day3->setFont(font);
            ui->label_high_day3->setText(forecast.high);
            ui->label_high_day3->setFont(font);
            ui->label_low_day3->setMinimumSize(50, 50);
            ui->label_high_day3->setMinimumSize(50, 50);
        }
        if(i==3){
            ui->label_week_day4->setText(forecast.week);
            ui->label_week_day4->setFont(font);
            ui->label_weather_icon_day4->setPixmap(m_weatherInfo.getForecastWeatherIcon(i, 50));
            ui->label_weather_icon_day4->setScaledContents(true);
            ui->label_low_day4->setText(forecast.low);
            ui->label_low_day4->setFont(font);
            ui->label_high_day4->setText(forecast.high);
            ui->label_high_day4->setFont(font);
            ui->label_low_day4->setMinimumSize(50, 50);
            ui->label_high_day4->setMinimumSize(50, 50);
        }
        if(i==4){
            ui->label_week_day5->setText(forecast.week);
            ui->label_week_day5->setFont(font);
            ui->label_weather_icon_day5->setPixmap(m_weatherInfo.getForecastWeatherIcon(i, 50));
            ui->label_weather_icon_day5->setScaledContents(true);
            ui->label_low_day5->setText(forecast.low);
            ui->label_low_day5->setFont(font);
            ui->label_high_day5->setText(forecast.high);
            ui->label_high_day5->setFont(font);
            ui->label_low_day5->setMinimumSize(50, 50);
            ui->label_high_day5->setMinimumSize(50, 50);
        }
        if(i==5){
            ui->label_week_day6->setText(forecast.week);
            ui->label_week_day6->setFont(font);
            ui->label_weather_icon_day6->setPixmap(m_weatherInfo.getForecastWeatherIcon(i, 50));
            ui->label_weather_icon_day6->setScaledContents(true);
            ui->label_low_day6->setText(forecast.low);
            ui->label_low_day6->setFont(font);
            ui->label_high_day6->setText(forecast.high);
            ui->label_high_day6->setFont(font);
            ui->label_low_day6->setMinimumSize(50, 50);
            ui->label_high_day6->setMinimumSize(50, 50);
        }
        if(i==6){
            ui->label_week_day7->setText(forecast.week);
            ui->label_week_day7->setFont(font);
            ui->label_weather_icon_day7->setPixmap(m_weatherInfo.getForecastWeatherIcon(i, 50));
            ui->label_weather_icon_day7->setScaledContents(true);
            ui->label_low_day7->setText(forecast.low);
            ui->label_low_day7->setFont(font);
            ui->label_high_day7->setText(forecast.high);
            ui->label_high_day7->setFont(font);
            ui->label_low_day7->setMinimumSize(50, 50);
            ui->label_high_day7->setMinimumSize(50, 50);
        }
    }
    
    // 将天气预报数据传递给 weather_lable 绘制折线图
       QList<Forecast> weather_List = m_weatherInfo.forecastList();
       weather_lable *weatherChart = findChild<weather_lable *>();  // 假设在 UI 里有这个控件，或直接通过指针关联
       if (weatherChart) {
           weatherChart->get_weather_temp(weather_List);
       }
    
}

